import java.io.*;

public class emailTest{	
	public String[] readFile(File f) throws Exception{
		//String line=null;
		String[] str= new String[10];
		FileReader fr=null;
		BufferedReader br=null;
		try{
			fr= new FileReader(f);
			br=new BufferedReader(fr);
			if((br.readLine())!=null){
				for(int i=0;i<str.length;i++){
					str[i]=br.readLine();
					//System.out.println(str[i]);
				}
				return str;	
			}
		}
		catch(FileNotFoundException e){
			e.printStackTrace();
		}
		catch(IOException e){
			e.printStackTrace();
		}
		finally{
			try {
				br.close();
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public String[] filter(String[] s,String p){
		String[] str=new String[10];
		boolean t;
			for(int i=0;i<s.length;i++){
				if(s[i]!=null)
				if((t=s[i].contains(p))){
					str[i]=s[i];
					//System.out.println(s[i]);
				}
			}
			return str;
	}
	
	public void writeFile(String[] s,File f){
		//String[] str= new String[5];
		FileWriter fr=null;
		BufferedWriter br=null;
		try{
			fr= new FileWriter(f);
			br=new BufferedWriter(fr);
			for(int i=0;i<s.length;i++){
				if((s[i])!=null){
					br.write(s[i]);
					br.newLine();
					//System.out.println(str[i]);
				}
			}
		}catch(FileNotFoundException e){
				e.printStackTrace();
		}catch(IOException e){
				e.printStackTrace();
		}
		finally{
			try {
				br.close();
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public void print(String[] s){
		for(int i=0;i<s.length;i++)
			System.out.println(s[i]);
	}
	
	public static void main(String[]ags) throws Exception{
		File f =new File("giri.txt");
		File f1=new File("gowda.txt");
		emailTest t=new emailTest();
		emailTest t1=new emailTest();
		emailTest t2=new emailTest();
		
		System.out.println("Before filter \n");
		String[] s=t.readFile(f);
		t.print(s);
		System.out.println();
		System.out.println("After filter \n");
		String[] s1=t1.filter(s,"gmail");
		t1.print(s1);
		t2.writeFile(s1,f1);
	}
}